var $ = require("../../utils/Ajax.js");
let WebIM = require("../../utils/WebIM")["default"];
let disp = require("../../utils/broadcast");

Page({
  data: {
    movies: [],

    // 用户自己的信息
    UserInfor: null,

    // 是否显示修改按钮
    EditBtn: false,

    friend_name: "",

    // 我的信息
    MyInfor: null
  },

  onLoad: function (options) {
    var that = this;
    var UserInfor = wx.getStorageSync('UserInfor');
    this.setData({
      MyInfor: UserInfor
    })
    if (UserInfor.id == options.Id){
      this.setData({
        EditBtn: true
      })
    }
    wx.setNavigationBarTitle({
      title: "个人资料"
    })

    let me = this;

    $.Ajax("api/uinfo", "GET", {
      id: options.Id
    }, function (res) {
      if (res.s) {
        that.setData({
          movies: res.d.album,
          UserInfor: res.d.user,
          friend_name: res.d.user.openId
        })
        console.log(res)


        WebIM.conn.subscribe({
          to: me.data.friend_name
        });

        // 判断当前是否存在该好友
        let rosters = {
          success: function (roster) {
            console.log('success')
            var member = [];
            for (let i = 0; i < roster.length; i++) {
              if (roster[i].subscription == "both") {
                member.push(roster[i]);
              }
            }
            if (me.isExistFriend(me.data.friend_name, member)) {
              me.toastFilled('已经是你的好友')
            }
            else {
              me.toastSuccess('已经发出好友申请')
            }
            me.setData({ isdisable: true })
          }
        };
        WebIM.conn.getRoster(rosters);



      } else {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500
        })
      }
    });
    
  },
  isExistFriend: function (name, list) {
    for (let index = 0; index < list.length; index++) {
      if (name == list[index].name) {
        return true
      }
    }
    return false
  },
  // 点击编辑资料
  GoModify: function () {
    wx.navigateTo({
      url: '../GoModify/GoModify',
    })
  },

  // 点击发起对话


  // 点击关注
  UserFollow: function(e){
    var that = this;
    var Id = e.currentTarget.dataset.id;
    $.Ajax("api/userfollow","POST",{
      id: Id
    },function(res){
      if(res.s){
        var UserInfor = that.data.UserInfor;
        UserInfor.isFriend = res.d.isFriend;
        that.setData({
          UserInfor: UserInfor
        })
      }
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500
      })
    })
  },
  	//	单聊
	into_singleChatRoom: function () {
    var that = this;
    var my = wx.getStorageSync("myUsername");
    var MyInfor = wx.getStorageSync('UserInfor');
    var nameList = {
      your: that.data.UserInfor.openId.toLowerCase(),
      yourTou: that.data.UserInfor.avatarUrl,
      myName: MyInfor.openId.toLowerCase(),
      myName_: MyInfor.nickName,
      myTou: MyInfor.avatarUrl,
      yourName: that.data.UserInfor.nickName,
    };

    var MsgListArr = [];
    var MsgUserList = wx.getStorageSync('MsgUserList');
    if (MsgUserList.length >= 1){
      for (var i = 0; i < MsgUserList.length;i++){
        if (MsgUserList[i].username == that.data.UserInfor.openId.toLowerCase()){
          MsgUserList.splice(i,1);
        }
      }
      MsgListArr = MsgUserList
    }

    
    MsgListArr.unshift({
      avatarUrl: that.data.UserInfor.avatarUrl,
      nickName: that.data.UserInfor.nickName,
      username: that.data.UserInfor.openId.toLowerCase(),
      time: '2019/5/7 17:03:45'
    });
    wx.setStorageSync('MsgUserList', MsgListArr);

    wx.navigateTo({
      url: "../SendMsg/SendMsg?username=" + JSON.stringify(nameList)
    });
  },
})